package com.niiwoo.civet.account.service.dubbo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.AccountCompany;
import com.niiwoo.civet.account.dto.request.CompanyAccountRequestDTO;
import com.niiwoo.civet.account.dto.response.AccountCompanyDTO;
import com.niiwoo.civet.account.enums.AccountCompanyAccountTypeEnum;
import com.niiwoo.civet.account.service.AccountCompanyDubboService;
import com.niiwoo.civet.account.service.local.AccountCompanyService;
import com.niiwoo.civet.user.service.OrganizationDubboService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 企业存管账户服务
 * @author TomXue
 * @since 2018-04-23
 */
@Service(version = "1.0.0")
@Slf4j
public class AccountCompanyDubboServiceImpl implements AccountCompanyDubboService{
    @Autowired
    AccountCompanyService accountCompanyService;
    @Reference(version = "1.0.0")
    OrganizationDubboService organizationDubboService;

    @Override
    public AccountCompanyDTO getCooperateOrgByOrgId(String orgId){
        AccountCompanyDTO accountCompanyDTO=null;
        AccountCompany accountCompany=accountCompanyService.queryAuditPassedAccountCompany(orgId,orgId, AccountCompanyAccountTypeEnum.COOPERATE_ORG.getType());
        if(accountCompany!=null){
            accountCompanyDTO=new AccountCompanyDTO();
            try {
                BeanUtils.copyProperties(accountCompanyDTO,accountCompany);
            } catch (Exception e) {
                log.error("根据机构ID查询合作机构信息发生系统异常,原因={}",e);
            }
        }

        return accountCompanyDTO;
    }

    @Override
    public AccountCompanyDTO getGuaranteeOrgByOrgId(String orgId) {
        AccountCompanyDTO accountCompanyDTO = null;
        AccountCompany accountCompany = accountCompanyService.queryAuditPassedAccountCompany(orgId, orgId, AccountCompanyAccountTypeEnum.GUARANTEE_ORG.getType());

        if (accountCompany != null) {
            accountCompanyDTO = new AccountCompanyDTO();
            try {
                BeanUtils.copyProperties(accountCompanyDTO, accountCompany);
            } catch (Exception e) {
                log.error("根据机构ID查询担保机构异常,orgId:{},",orgId, e);
            }
        }

        return accountCompanyDTO;
    }

    @Override
    public AccountCompanyDTO getInvestOrgByOrgId(String orgId) {
        AccountCompanyDTO accountCompanyDTO = null;
        AccountCompany accountCompany = accountCompanyService.queryAuditPassedAccountCompany(orgId, orgId, AccountCompanyAccountTypeEnum.INVEST_ORG.getType());

        if (accountCompany != null) {
            accountCompanyDTO = new AccountCompanyDTO();
            try {
                BeanUtils.copyProperties(accountCompanyDTO, accountCompany);
            } catch (Exception e) {
                log.error("查询企业投资人存管开户信息,机构ID={},发生系统异常,原因={},",orgId, e);
            }
        }

        return accountCompanyDTO;
    }

    @Override
    public boolean isOpenAccountCompanyByOrgCode(String orgId) {
        return getCooperateOrgByOrgId(orgId)==null?false:true;
    }

    @Override
    public AccountCompanyDTO getAccountCompanyByBusinessLicense(String businessLicense) {
        AccountCompanyDTO accountCompanyDTO=new AccountCompanyDTO();
        try {
            AccountCompany accountCompany = accountCompanyService.queryAccountCompanyByBusinessLicense(businessLicense);
            if(accountCompany == null) {
                return null;
            }
            BeanUtils.copyProperties(accountCompanyDTO, accountCompany);
        } catch (Exception e){
            log.error("根据营业执照编码查询机构信息,编码={},发生系统异常,原因={}",businessLicense,e);
            throw new RuntimeException(e);
        }

        return accountCompanyDTO;
    }

    @Override
    public boolean isOpenAccountCompanyInOrg(String orgId, String companyOrgId) {
        return accountCompanyService.queryByOrgIdAndCompanyOrgIdWithAccountType(orgId, companyOrgId,AccountCompanyAccountTypeEnum.BORROW_ORG.getType()) != null;
    }

    @Override
    public boolean updateAccountCompany(CompanyAccountRequestDTO reqDto){
        return accountCompanyService.updateAccountCompany(reqDto);
    }

    @Override
    public AccountCompanyDTO queryAccountCompanyByUserId(String orgId) {
        AccountCompanyDTO accountCompanyDTO=new AccountCompanyDTO();
        AccountCompany accountCompany=accountCompanyService.queryAccountCompanyByOrgId(orgId);

        try {
            BeanUtils.copyProperties(accountCompanyDTO, accountCompany);
        } catch (Exception e) {
            log.error("查询企业帐户信息,机构ID={},发生系统异常,原因={}", orgId, e);
            throw new RuntimeException(e);
        }

        return accountCompanyDTO;
    }

    public AccountCompanyDTO queryAccountCompanyByUserIdAndOrgIdWithAccountType(String userId,String orgId,Byte accountType) {
        AccountCompanyDTO accountCompanyDTO=new AccountCompanyDTO();
        AccountCompany accountCompany=accountCompanyService.queryAccountCompanyByOrgIdAndUserIdWithAccountType(userId,orgId,accountType);

        if( accountCompany != null ) {
            try {
                BeanUtils.copyProperties(accountCompanyDTO, accountCompany);
                return accountCompanyDTO;
            } catch (Exception e) {
                log.error("查询企业帐户信息,用户ID={},机构ID={},发生系统异常,原因={}", userId,orgId, e);
                throw new RuntimeException(e);
            }
        }else{
            return null;
        }

    }
}
